package com.taomu.app.zongchouhuli.http.api;

import android.util.Log;

import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseErrorResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.utils.ParseUtil;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;

public class FlatMapNotDataObserver implements Function<ResponseBody, Observable<String>> {

    private final String code = "200";

    private final String token = "411";

    @Override
    public Observable<String> apply(ResponseBody responseBody) throws Exception {
        String json = responseBody.string();
        Log.d("dwqdwqdwq", "json:"+json);
        BaseErrorResponse baseErrorResponse = ParseUtil.parseObject(json, BaseErrorResponse.class);
        if(code.equals(baseErrorResponse.getRet())){
            return Observable.just(baseErrorResponse.getMsg());
        }else if("411".equals(baseErrorResponse.getRet())){
            return Observable.error(new Throwable("411"));
        }else{
            return Observable.error(new Throwable(baseErrorResponse.getMsg()));
        }
    }
}
